<?php


namespace app\admin\controller\Admin;


use app\admin\controller\BaseController;
use app\admin\model\Admin\MenuModel;
use app\Utils\Util;
use think\Request;

class Menu extends BaseController
{

    /**
     * 分页查询
     * @param Request $request
     * @return array
     */
    public function getMenuList(Request $request)
    {
        $param = $request->param();
        $where = [];
        $page = $param['page'];
        $limit = $param['limit'];
        $pid = $param['pid'];
        $menu_name = $param['menu_name'];
        $is_show = $param['is_show'];
        if(empty($page)){
            $page = 1;
        }
        if(empty($limit)){
            $limit = 10;
        }

        if(!empty($pid)){
            array_push($where,['pid','=',$pid]);
        }else{
            array_push($where,['pid','=',0]);
        }

        if(!empty($menu_name)){
            array_push($where,['menu_name','like','%'.$menu_name.'%']);
        }
        if(!empty($is_show)){
            if($is_show == 1){
                array_push($where,['is_show','=',1]);
            }
            if($is_show == 2){
                array_push($where,['is_show','=',0]);
            }
        }
        $menus = MenuModel::getMenuListWhere($page,$limit,$where);
        $count = MenuModel::getMenuCountWhere($where);
        return Util::returnPageJson(self::CODE_DATA,$count,$menus,"");

    }

    /**
     * 添加
     * @param Request $request
     * @return array
     */
    public function addMenu(Request $request)
    {
        $param = $request->param();
        $menu_name = $param['menu_name'];
        $pid = $param['pid'];
        $icon = $param['icon'];
        $is_show = $param['is_show'];
        $data = [
            'menu_name'=>$menu_name,
            'pid'=>$pid
        ];
        if(!empty($is_show) && $is_show == "on"){
            $data['is_show'] = 1;
        }
        if(!empty($icon)){
            $data['icon'] = $icon;
        }
        $id = MenuModel::addMenu($data);
        if($id){
            return Util::returnJson(self::CODE_SUCCESS,$id,"添加成功");
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"添加失败");
        }
    }

    /**
     * 一级节点
     * @param Request $request
     * @return array
     */
    public function getNodeMenu(Request $request)
    {
        $menus = MenuModel::getMenuWhereMore([['pid','=',0]]);
        return Util::returnJson(self::CODE_SUCCESS,$menus,"");
    }

    /**
     * 更新
     * @param Request $request
     * @return array
     */
    public function editMenu(Request $request)
    {
        $param = $request->param();
        $id = $param['id'];
        $menu_name = $param['menu_name'];
        $pid = $param['pid'];
        $icon = $param['icon'];
        $is_show = $param['is_show'];
        if(empty($id)){
            return Util::returnJson(self::CODE_ERROR,null,"参数错误");
        }
        if(!empty($menu_name)){
            $save['menu_name'] = $menu_name;
        }
        if(!empty($pid)){
            $save['pid'] = $pid;
        }else{
            if(isset($pid) && $pid == 0){
                $save['pid'] = $pid;
            }
        }
        if(!empty($icon)){
            $save['icon'] = $icon;
        }
        if(!empty($is_show)){
            if($is_show == "on"){
                $save['is_show'] = 1;
            }else{
                $save['is_show'] = 0;
            }
        }
        $flag = MenuModel::updateMenuWhere([['id','=',$id]],$save);
        if($flag){
            return Util::returnJson(self::CODE_SUCCESS,null,"修改成功");
        }else{
            return Util::returnJson(self::CODE_ERROR,null,"修改失败");
        }
    }
}
